﻿@using KMF.WebUI.Areas.KMFadmin.Models
@using KMF.WebUI.Areas.KMFadmin.CustomHelpers
@model IEnumerable<KMF.Common.OrderPagingDto>
@{
    ViewBag.Title = "Lọc đơn hàng";
    Layout = "~/Areas/KMFadmin/Views/Shared/_layout.cshtml";
}

<link href="~/Areas/KMFadmin/Content/css/browerImage.css" rel="stylesheet" />
<link href="~/Areas/KMFadmin/Scripts/jQueryDatePicker/css/ui-lightness/jquery-ui-1.10.1.custom.min.css" rel="stylesheet" />
<link href="~/Areas/KMFadmin/Scripts/jQueryDatePicker/js/jquery-ui-timepicker-addon.css" rel="stylesheet" />
@section LeftMenu
{
    <div id="left">
        <div class="LeftMenuWraper">
            <div class="leftMenuHeader">
                <span style="padding-left: 10px;">
                    <strong>Truy cập nhanh</strong>
                </span>
            </div>
            <ul id="p-quick-menu" class="quick-menu">
                <li class="qmn-info-pro">
                    <a href="@Url.Action("Index", "AdminCustomer")">Khách hàng</a>
                </li>
                <li class="qmn-seo-pro">
                    <a href="@Url.Action("Index", "AdminOrder")">Đơn hàng</a>
                </li>
            </ul>
            <div class="leftMenuFooter"></div>
        </div>
    </div>
    <!-- END #left -->
}
<nav>
    <div id="jCrumbs" class="breadCrumb module">
        <ul>
            <li><a href="@Url.Action("Index","AdminHome")"><i class="bootstrap icon-home"></i></a></li>
            <li><a href="javascript:void(0)">Danh sách đơn hàng</a> </li>
        </ul>
    </div>
</nav>
<div class="toolBox">
    @Html.ToolBox(new List<object> { new Back { ClassCss = "Back", ActionName = "/KMFadmin/AdminOrder" } })
</div>
<div>
    @using (Html.BeginForm("Filter", "Order", new { StartDate = ViewBag.StartDate, EndDate = ViewBag.EndDate }))
    {
        <span>Từ ngày:</span><input type="text" value="@ViewBag.StartDate" style="width:150px;" id="StartDate" name="StartDate" />
        <span>Đến ngày:</span><input type="text" value="@ViewBag.EndDate" style="width:150px;" id="EndDate" name="EndDate" />
        <input type="submit" value="Lọc" />
        <span>&nbsp;Lọc nhanh:</span>
        @Html.DropDownList("ddlFilOrder", new SelectList((List<object>)ViewBag.ListOption, "ItemValue", "ItemText"))
    }
</div>

@{
    DateTime sDate = Convert.ToDateTime(ViewBag.StartDate);
    DateTime eDate = Convert.ToDateTime(ViewBag.EndDate);
    while (eDate >= sDate)
    {
        //date.Add(sDate);
        @*@eDate.ToString("dd/MM/yyy")<br />*@
        var modelByDate = from o in Model where o.ReceiveDate.Day == eDate.Day orderby o.ReceiveDate descending select o;
        decimal total = 0;
        <div style="margin-top:30px;">
            @eDate.ToString("dd/MM/yyy")
        </div>
        <table class="table-odd table-plist">
            <tr>
                <th style="width:30px;">STT</th>
                <th style="width:80px;">Mã</th>
                <th>Người đặt</th>
                <th style="width:100px;">Ngày đặt</th>
                <th style="width:100px;">Tổng tiền</th>
                <th style="width:60px;">Trạng thái</th>
            </tr>
            
            @if (modelByDate.Count() > 0)
            {
                foreach (var item in modelByDate)
                {
                    total += item.TotalMoney;
                    <tr>
                        <td>@item.STT</td>
                        <td>@item.OrderId</td>
                        <td>
                            <a href="">@item.FullName</a>
                        </td>
                        <td>
                            @item.ReceiveDate.ToString("dd/MM/yyyy")
                        </td>
                        <td>@item.TotalMoney.ToString("##,###")</td>
                        <td style="text-align:center;">
                            <a href="@Url.Action("Detail", "AdminOrder", new { id = @item.OrderId })">Chi tiết</a>
                        </td>
                    </tr>
                }
            }
        </table>
    <div style="text-align:right;">
        <strong>Tổng: </strong><span style="color:Red;">@total.ToString("##,###")</span> vnđ
    </div>
            eDate = eDate.AddDays(-1);
    }

}
@section scripts
{
    <script src="@Url.Content("~/Areas/KMFadmin/Scripts/jQueryDatePicker/js/jquery-ui-1.10.1.custom.min.js")"></script>
    <script src="@Url.Content("~/Areas/KMFadmin/Scripts/jQueryDatePicker/js/jquery-ui-timepicker-addon.js")"></script>
    <script type="text/javascript">
        function DeleteOrder(id) {
            $.ajax({
                url: '/KMFadmin/AdminOrder/DeleteOrder',
                data: { id: id },
                success: function (data) {
                    if (data) {
                        alert('Xóa khách hàng thành công!');
                        window.location = '/KMFadmin/AdminOrder';
                    } else {
                        alert('Xóa khách hàng thất bại!');
                    }
                },
                error: function () {
                    alert('Đã có lỗi xảy ra!');
                }
            });
        }

        $('select#ddlFilOrder').change(function () {
            var data = $(this).val();
            //ngay hien tai
            var e = new Date(); //nay ket thuc
            var eDate = e.getDate() + '/' + (e.getMonth() + 1) + '/' + e.getFullYear();
            var s = new Date(); //ngay bat dau
            var param = '';
            switch (data) {
                case 'today':
                    param += '?StartDate=' + eDate + '&EndDate=' + eDate;
                    break;
                case 'yesterday':
                    s.setDate(s.getDate() - 1);
                    e.setDate(e.getDate() - 1);
                    var sDate = s.getDate() + '/' + (s.getMonth() + 1) + '/' + s.getFullYear();
                    eDate = e.getDate() + '/' + (e.getMonth() + 1) + '/' + e.getFullYear();
                    param += '?StartDate=' + sDate + '&EndDate=' + eDate;
                    break;
                case 'Lastweek':
                    s.setDate(s.getDate() - 7);
                    var sDate = s.getDate() + '/' + (s.getMonth() + 1) + '/' + s.getFullYear();
                    param += '?StartDate=' + sDate + '&EndDate=' + eDate;
                    break;
                case 'LastMonth':
                    e.setDate(e.getMonth() - 2);
                    var eDate = e.getDate() + '/' + (e.getMonth() + 1) + '/' + e.getFullYear();
                    var sDate = '01/' + (e.getMonth() + 1) + '/' + s.getFullYear();
                    param += '?StartDate=' + sDate + '&EndDate=' + eDate;
                    break;
            }

            if (data == '0') {
                location.href = '/KMFadmin/AdminOrder/index';
            } else {

                location.href = '/KMFadmin/AdminOrder/Filter/' + param;
            }
        });
        // ------------- Date picker -------
        $('#StartDate').datetimepicker({ dateFormat: 'dd/mm/yy', timeFormat: "hh:mm:ss tt", timeOnlyTitle: "Chọn thời gian", showTimepicker: false });
        $('#EndDate').datetimepicker({ dateFormat: 'dd/mm/yy', timeFormat: "hh:mm:ss tt", timeOnlyTitle: "Chọn thời gian", showTimepicker: false });
    </script>
}
